Skip to content

Draw play_movie frame number on an 8-bit mask for OpenCV 5 - #1592

Open
arxigen wants to merge 1 commit into
flatironinstitute:devfrom
arxigen:fix/movie-play-opencv5
Open

arxigen wants to merge 1 commit into
flatironinstitute:devfrom
arxigen:fix/movie-play-opencv5

Conversation

@arxigen

@arxigen arxigen commented Oct 26, 2026

Copy link
Copy Markdown

Description

movie.play(plot_text=True) fails on OpenCV 5 with (-215:Assertion failed) img.depth() == CV_8U in function 'cv::putText'. play_movie normalizes each frame to floating point before drawing the frame number, and from OpenCV 5 on cv2.putText only draws on 8-bit images.

This draws the text into a uint8 mask and sets those pixels to 255 in the float frame, which is the value the old call wrote. On OpenCV 4 the displayed frames are byte-identical before and after. OpenCV 5 antialiases text, but partial coverage saturates to white on the 0-1 display scale anyway.

I saw your note on the issue about looking for a good alternative for text rendering. This is one small option that keeps putText and the current look, so feel free to take it, change it or close it if you'd rather go another way.

compute_flow_single_frame in motion_correction.py (with play_flow) has the same pattern. I left it alone to keep this small and can follow up if you like.

Fixes #1589

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Branching

Targets dev.

Has your PR been tested?

Added test_play_plot_text to caiman/tests/test_movies.py. It runs the opencv backend headless by recording cv2.imshow. It fails without the change on OpenCV 5.0.0 and passes with it on 5.0.0 and 4.14.0. I also checked that the frames shown on OpenCV 4.14 are identical before and after. I ran only the movie tests (Windows, Python 3.13), not the full caimanmanager test or demotest.

cv2.putText only accepts 8-bit images from OpenCV 5 on, but play_movie
normalizes frames to floating point before drawing the frame number, so
movie.play(plot_text=True) failed with an assertion in the demos. Render
the text into a uint8 mask and copy it onto the frame instead. Output is
unchanged on OpenCV 4.

Add a headless test that plays a small movie with plot_text=True.
@pgunn

pgunn commented Oct 31, 2026

Copy link
Copy Markdown
Member

Thanks for this; sorry I've been so slow to dig into it. Been very busy at work. Will try to dig in this weekend. I really appreciate your effort to tackle it.

@pgunn

pgunn commented Nov 7, 2026

Copy link
Copy Markdown
Member

This is good work! thanks

@pgunn

pgunn commented Nov 14, 2026

Copy link
Copy Markdown
Member

I'll return to this after I fix the CI infra

@pgunn

pgunn commented Nov 14, 2026

Copy link
Copy Markdown
Member

Hey sorry to make a fuss on this; do you mind rebasing? The CI was broken for a bit and I think a rebase will correctly test it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants